local dingwang = fk.CreateSkill{
  name = "wk_heg__dingwang",
  tags = {Skill.MainPlace},
}

local H = require "packages.ol_hegemony.util"
local HG = require "packages.new_heg_diy_my.util"


Fk:loadTranslationTable{
  ["wk_heg__dingwang"] = "定王",
  [":wk_heg__dingwang"] = "主将技，与你势力相同的角色使用点数为K的牌时，你可令一名场上牌点数最大的角色选择叠置或本回合不能使用手牌，然后你交换主副将。",

  ["#wk_heg__dingwang_choose"] = "定王：你可以选择一名场上牌点数最大的角色，然后其选择叠置或本回合不能使用手牌",
  ["@@wk_heg__dingwang_nouse-turn"] = "定王 禁用手牌",
  ["wk_heg__dingwang_turnover"] = "叠置",
  ["wk_heg__dingwang_nouse"] = "不能使用手牌",
 
  ["$wk_heg__dingwang1"] = "历经磨难，不改佐国之志。",
  ["$wk_heg__dingwang2"] = "建功立业，唯愿天下早定。",
}

dingwang:addEffect(fk.CardUsing, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return H.compareKingdomWith(player, target) and player:hasSkill(dingwang.name) and data.card.number == 13
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local min_num = -1
    for _, p in ipairs(room.alive_players) do
      for _, cid in ipairs(p:getCardIds("ej")) do
        local card = Fk:getCardById(cid)
        if card.number > min_num then
          min_num = card.number
        end
      end
    end
    if min_num == -1 then return false end
    local targets = {}
    for _, p in ipairs(room.alive_players) do
      if table.find(p:getCardIds("ej"), function (cid) return Fk:getCardById(cid).number == min_num end) then
        table.insert(targets, p)
      end
    end
    local to = room:askToChoosePlayers(player,{
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#wk_heg__dingwang_choose",
      skill_name = dingwang.name,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {to = to[1]})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to
    local choices = {"wk_heg__dingwang_turnover", "wk_heg__dingwang_nouse"}
    local choice = room:askToChoice(to, {choices=choices, skill_name=dingwang.name})
    if choice == "wk_heg__dingwang_turnover" then
      to:turnOver()
    else
      room:setPlayerMark(to, "@@wk_heg__dingwang_nouse-turn", 1)
    end
    HG.SwapMainAndDeputy(room, player)
  end
})

dingwang:addEffect("prohibit",{
    prohibit_use = function(self, player, card)
       if player:getMark("@@wk_heg__dingwang_nouse-turn") == 0 then return false end
       local subcards = Card:getIdList(card)
       return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
       end)
    end,
})

-- dingwang:addEffect(fk.Damaged,{
--   anim_type = "masochism",
--   events = {fk.Damaged},
--   can_trigger = function(self, event, target, player, data)
--     return H.compareKingdomWith(player, target) and player:hasSkill(dingwang.name)
--      and (player:hasShownSkill(dingwang.name) or player == target)
--      and not table.find(player.room.alive_players, function (p)
--         return H.compareKingdomWith(p, target) and p.hp > target.hp
--      end)
--   end,
--   on_cost = function(self, event, target, player, data)
--     local room = player.room
--     local to = room:askToChoosePlayers(target,{
--       targets = room:getOtherPlayers(target),
--       min_num = 1,
--       max_num = 1,
--       prompt = "#hs__fangzhu-choose:::"..target:getLostHp(),
--       skill_name = dingwang.name,
--       cancelable = true,
--     })
--     if #to > 0 then
--       event:setCostData(self, {to = to[1]})
--       return true
--     end
--   end,
--   on_use = function(self, event, target, player, data)
--     local room = player.room
--     local to = event:getCostData(self).to
--     local num = target:getLostHp()
--     if to.hp > 0 and #room:askToDiscard(to, {
--       min_num = num,
--       max_num = num,
--       include_equip = true,
--       skill_name = dingwang.name,
--       prompt = "wk_heg__fangzhu_ask",
--       cancelable = true,
--     }) > 0 then
--       if not to.dead then 
--         room:loseHp(to, 1, dingwang.name)
--       end
--     else
--       to:drawCards(num, dingwang.name)
--       if not to.dead then
--         to:turnOver()
--       end
--     end
--     HG.SwapMainAndDeputy(room, player)
--   end,
-- })

return dingwang